<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}
if($_POST['sub']!=''){
    

    $catid=intval($_POST["catid"]);
    $id=empty($_POST['id'])?0:(int)$_POST['id'];
    if(empty($catid)){
        jumpajax('300','没有选择发布栏目','');
    }    

    $categoryinfo=$ezhand->select("category",'*'," id=".$catid);
    if(empty($categoryinfo)){
       jumpajax('300','栏目不存在!',''); 
    } 
    $catename=$categoryinfo["catname"];
    $modelid=$categoryinfo["modelid"];
    $liststyle=$categoryinfo["liststyle"];
    $workflowid=$categoryinfo["workflowid"];   
    if(empty($modelid)){
        jumpajax('300','请进入单页面发布..','');
        
    }

    $tablename=$ezhand->select("model",'tablename','id="'.$modelid.'"');
    $row=$ezhand->select_all("modelfield",'id,field,formtype'," modelid=".$modelid." and disabled=0 and issys=0 order by listorder desc, id desc");
    $title=trim($_POST["title"]);
    $keywords=trim($_POST["keywords"]);
    $aid=intval($_POST["thumb_id"]);
    $stid=intval($_POST["stid"]);
  
    $threadinfo=array(
    "catid"=>$catid,
    "title"=>$title,
    "thumb"=>$_POST["thumb_fileName"],
    "keywords"=>$keywords,
    "modelid"=>$modelid,
    "uid"=>$_SESSION['EZ_Admin']['id'],    
    "username"=>$_SESSION['EZ_Admin']['name'],
    "updatetime"=>time(),
    "islink"=>0,
    "listorder"=>intval($_POST["listorder"]),
    "url"=>trim($_POST["url"]),
    "description"=>$_POST["description"],
    "ip"=>get_client_ip()
    );
    $linkspecial_field="";
    $category_field="";
    $position_field=$_POST["posids_id"];

    $idslist=array();
    $nameslist=array(); 
    $titleslist=array(); 
    
    $urlslist=array(); 
    $utslist=array();  
    $urlslist2=array(); 
    $utslist2=array();              
    if(!empty($row)){
        foreach ($row as $r){
         $field=$r["field"];
         if($r["formtype"]=="category"){
            $postdata[$field]=$_POST[$field."_id"];
            $category_field=$field."_id";
         }elseif($r["formtype"]=="linkspecial"){
            $postdata[$field]=$_POST[$field."_id"];
            $linkspecial_field=$field."_id";
         }elseif($r["formtype"]=="password"){
            $pwd=trim($_POST[$field]);
            if($pwd<>""){
              $postdata[$field]=md5($pwd);
            }else{
               unset($postdata[$field]); 
            }
         }elseif($r["formtype"]=="linkcat"){
            
            $postdata[$field]=$_POST[$field."_id"];
            $linkcat_field=$field."_id";            
          }elseif($r["formtype"]=="box"){
            if(empty($_POST[$field])){
                $postdata[$field]="";
            }else{
                $postdata[$field]=serialize($_POST[$field]);
            }
         }elseif($r["formtype"]=="province"){
            
            
            $postdata[$field."_province"]=$_POST["province"];
            $postdata[$field."_city"]=$_POST["city"];
            $postdata[$field."_district"]=$_POST["district"];
            
            $postdata[$field]=serialize(array("province"=>$postdata[$field."_province"],"city"=>$postdata[$field."_city"],"district"=>$postdata[$field."_district"]));
         
         }elseif($r["formtype"]=="images"){
          $i=0;
          
          $imagedata=array();  
          while($i<10){
            $imageid=$field."".$i."_id";
            $imagename=$field."".$i."_fileName";
            $imagetitle=$field."".$i."_title";
            if($_POST[$imageid]<>""){
             $idslist[]=$_POST[$imageid];
             $nameslist[]=$_POST[$imagename]; 
             $titleslist[]=$_POST[$imagetitle];
            }          

            $i++;      
          }
          $imagedata["ids"]=$idslist;
          $imagedata["names"]=$nameslist;
          $imagedata["titles"]=$titleslist;          
          $postdata[$field]=serialize($imagedata);  
         }elseif($r["formtype"]=="links"){
          $i=0;
          $relatelinkdata=array();  
          while($i<10){
            $imagename=$field."".$i."_title";
            $imagetitle=$field."".$i."_url";
            if($_POST[$imagename]<>""){
             $utslist[]=$_POST[$imagename]; 
             $urlslist[]=$_POST[$imagetitle];
            }          

            $i++;      
          }
          $relatelinkdata["titles"]=$utslist;
          $relatelinkdata["urls"]=$urlslist;          
          $postdata[$field]=serialize($relatelinkdata);             
         }elseif($r["formtype"]=="attribute"){
            
          $i=0;
          $relateattdata=array();  
          while($i<15){
            $imagename=$field."".$i."_title";
            $imagetitle=$field."".$i."_val";
            if($_POST[$imagename]<>""){
             $utslist2[]=$_POST[$imagename]; 
             $urlslist2[]=$_POST[$imagetitle];
            }          

            $i++;      
          }
          $relateattdata["titles"]=$utslist2;
          $relateattdata["vals"]=$urlslist2;          
          $postdata[$field]=serialize($relateattdata);
                      
          
         }else{
            $postdata[$field]=$_POST[$field];   
         }
         
         
       }    
    }
    $postdata["title"]=$title;

    if(empty($threadinfo["thumb"])){
         if(!empty($nameslist)){
            $threadinfo["thumb"]=$nameslist[0];
            $aid=$idslist[0];        
         }            
    }
            
    if($id==0){
        $threadinfo["inputtime"]=time();
        if($workflowid==0){
            $threadinfo["status"]=99;
        }else{
            $threadinfo["status"]=1;
        }
        if($stid==0){
            $threadinfo["status"]=0;
        }

        $tid=$ezhand->insert("thread",$threadinfo);
        if(!empty($idslist)){
            while(list($k2,$v2)=each($idslist)){
            $attlist=array("tid"=>$tid,
            "aid"=>$v2
            );
            $ezhand->insert("attachment_index",$attlist);                
                
            }
            
        }
        if(!empty($aid)){
            $attlist=array("tid"=>$tid,
            "aid"=>$aid
            );
            $ezhand->insert("attachment_index",$attlist);
        }
        
        $postdata["tid"]=$tid;
        
	    $pid=$ezhand->insert($tablename,$postdata);
        
        if(!empty($linkspecial_field)){
            set_linkspecial($tid,$_POST[$linkspecial_field]);
        }
        if(!empty($category_field)){
            set_relatecat($tid,$_POST[$category_field]);
        }   
        if(!empty($position_field)){
            set_positions($tid,$position_field,$catid,$modelid);    
        }             
        //
         if($modelid>0){
            $uinfo =array ('f' => 'num','e' => '+','v' =>1);
            $ezhand->update("model",$uinfo,'id="'.$modelid.'"');
         }
         
		jumpajax('200','保存成功','page'.$catid,'closeCurrent');
                
        
    }else{
        $tid=$id;
        if(!empty($aid)){
            $ezhand->delete('attachment_index','tid="'.$tid.'"');
            $attlist=array("tid"=>$tid,
            "aid"=>$aid
            );
            $ezhand->insert("attachment_index",$attlist);
        }	 
        if($workflowid==0){
            $threadinfo["status"]=99;
        }else{
            $threadinfo["status"]=1;
        }
        if($stid==0){
            $threadinfo["status"]=0;
        }                  
        $ezhand->update("thread",$threadinfo,'id="'.$id.'"');
        if(!empty($idslist)){
            while(list($k2,$v2)=each($idslist)){
            $attlist=array("tid"=>$tid,
            "aid"=>$v2
            );
            $ezhand->insert("attachment_index",$attlist);                
                
            }
            
        }        
        $data["tid"]=$id;
	    $ezhand->update($tablename,$postdata,'tid="'.$id.'"');
		
        if(!empty($linkspecial_field)){
            set_linkspecial($id,$_POST[$linkspecial_field]);
        }
        if(!empty($category_field)){
            set_relatecat($id,$_POST[$category_field]);
        }
        if(!empty($position_field)){
            set_positions($id,$position_field,$catid,$modelid);    
        }
        
        
                        
        
        jumpajax('200','修改成功','page'.$catid,'closeCurrent');
                
        
        
    }
    
        
    
    
    
}else{
    $catid=intval($_GET["catid"]);
    $id=empty($_GET['id'])?0:(int)$_GET['id'];
    if(empty($catid)){
        jump('300','没有选择发布栏目','');
    }

    $categoryinfo=$ezhand->select("category",'*'," id=".$catid);
    if(empty($categoryinfo)){
       jump('300','栏目不存在!',''); 
    }    
    $catename=$categoryinfo["catname"];
    $modelid=$categoryinfo["modelid"];
    $liststyle=$categoryinfo["liststyle"];
    if(empty($modelid)){
        jump('index.php?mod=content&act=list&catid='.$catid,'请进入单页面发布..','');
        
    }
    $tablename=$ezhand->select("model",'tablename','id="'.$modelid.'"');
    //$tablename=$ezhand->TABLEPRE.$tablename;
        
    $row=$ezhand->select_all("modelfield",'*'," modelid=".$modelid." and disabled=0 order by listorder asc, id desc");
    $posids="";
    $displayval="";
    if(!empty($id)){
        $infodata=$ezhand->select("thread t,".$tablename." d",'t.*,d.*','t.id=d.tid and t.id="'.$id.'"');
        
     	$hd_arr = $ezhand->select_all('position p,position_data d','p.id,p.title','p.id=d.posid and d.tid='.$id);
        $list1=array();
        $list2=array();
        foreach ( $hd_arr as $posrow ) {
            $list1[]=$posrow["title"]; 
            $list2[]=$posrow["id"];
        }
        $displayval=implode(",",$list1);
        $posids=implode(",",$list2);   
        
    }
    
    
}



?>